package com.nd.controller.admin;

import com.nd.Utils.ValidUtil;
import com.nd.pojo.bean.Comment;
import com.nd.pojo.bean.Goods;
import com.nd.pojo.bo.Reply;
import com.nd.pojo.bo.UpdateGoodsBo;
import com.nd.pojo.vo.*;
import com.nd.service.GoodsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("admin")
public class GoodsController {

    @Autowired
    GoodsService goodsService;
    //查询商品列表接口
    @RequestMapping("goods/list")
    //访问该url需要具有的权限限制
    @RequiresPermissions({"admin:goods:list"})
    public BaseRespVo list(Integer page,Integer limit,String sort,String order,String goodsSn,String name){
        GoodsListVo goodsListVo = goodsService.queryGoodsListVo(page,limit,sort,order,goodsSn,name);
        return BaseRespVo.ok(goodsListVo);
    }

    @RequestMapping("goods/delete")
    //逻辑删除商品
    public BaseRespVo delete(@RequestBody Goods goods){
        int delete = goodsService.deleteGoods(goods);
        if(delete == 0) return BaseRespVo.fail();
        return BaseRespVo.ok();
    }

    @RequestMapping("goods/detail")
    //查询商品详情
    public BaseRespVo detail(Integer id){
        GoodsDetailVo goodsDetailVo = goodsService.queryGoodsDetailById(id);
        return BaseRespVo.ok(goodsDetailVo);
    }

    @RequestMapping("goods/catAndBrand")
    //类目关联查询
    public BaseRespVo catAndBrand(){
        CatAndBrandVo catAndBrandVo = goodsService.queryCatAndBrandVo();
        return BaseRespVo.ok(catAndBrandVo);
    }

    @RequestMapping("goods/update")
    //更新商品
    public BaseRespVo update(@Validated @RequestBody UpdateGoodsBo updateGoodsBo, BindingResult bindingResult){
        if(bindingResult.hasFieldErrors()) {
            return ValidUtil.valid(bindingResult);
        }
        int update = goodsService.updateGoods(updateGoodsBo);
        if(update == 9999) return BaseRespVo.fail("商品编号和商品名称组合已经存在！");
        if(update == 9998) return BaseRespVo.fail("商品编号和商品名称不能为空！");
        if(update == 9997) return BaseRespVo.fail("价格和库存不能为负数！");
        if(update == 10000) return BaseRespVo.fail();
        return BaseRespVo.ok();
    }
    @RequestMapping("goods/create")
    //上架商品
    public BaseRespVo create(@Validated @RequestBody UpdateGoodsBo updateGoodsBo){
        int add = goodsService.createGoods(updateGoodsBo);
        if(add == 9999) return BaseRespVo.fail("商品编号和商品名称组合已经存在！");
        if(add == 9998) return BaseRespVo.fail("商品编号和商品名称不能为空！");
        if(add == 9997) return BaseRespVo.fail("价格和库存不能为负数！");
        if(add == 10000) return BaseRespVo.fail();
        return BaseRespVo.ok();
    }
    //查询商品评论
    @RequestMapping("comment/list")
    public BaseRespVo commentList(Integer page,Integer limit,String sort,String order,Integer userId,Integer valueId){
        CommentsListVo commentsListVoVo = goodsService.queryGoodsCommentsListVo(page,limit,sort,order,userId,valueId);
        return BaseRespVo.ok(commentsListVoVo);
    }
    //删除商品评论
    @RequestMapping("comment/delete")
    public BaseRespVo deleteComment(@RequestBody Comment comment){
        int delete = goodsService.deleteComment(comment);
        if(delete == 10000) return BaseRespVo.fail();
        return BaseRespVo.ok();
    }
    //回复订单
    @RequestMapping("order/reply")
    public BaseRespVo replyComment(@RequestBody Reply reply){
        return BaseRespVo.fail(662,"订单已回复");
    }


}
